<?php
namespace Amoysea\Controller;
use Think\Controller;
class EmsController extends BaseController {
    public function _initialize()
    {
        parent::_initialize();
        $g=session('company');
		C('erp_dbname',$g['db']);
        $db_name=$g['db'];
		//数据库连接基础配置-begin
		C('db_config.DB_TYPE',C('DB_TYPE'));
		C('db_config.DB_HOST',C('DB_HOST'));
		C('db_config.DB_USER',C('DB_USER'));
		C('db_config.DB_PWD',C('DB_PWD'));
		C('db_config.DB_PORT',C('DB_PORT'));
		C('db_config.DB_CHARSET',C('DB_CHARSET'));
		C('db_config.DB_NAME',C('erp_dbname'));
		//数据库连接基础配置-end
    }
	public function curl_web($url1,$url2){
		$curl = curl_init(); // 启动一个CURL会话
		curl_setopt($curl, CURLOPT_URL, $url1); // 要访问的地址           
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
		curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
		//curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
		curl_setopt($curl, CURLOPT_REFERER, url2);
		curl_setopt($curl, CURLOPT_HTTPGET, 1); // 发送一个常规的Post请求
		curl_setopt($curl, CURLOPT_COOKIEJAR, $GLOBALS['cookie_file']); // 存放Cookie信息的文件名称
		curl_setopt($curl, CURLOPT_COOKIEFILE,$GLOBALS ['cookie_file']); // 读取上面所储存的Cookie信息
		curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
		curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
		$tmpInfo = curl_exec($curl); // 执行操作
		if (curl_errno($curl)) {
		   echo '操作超时!';
		}
		curl_close($curl); // 关键CURL会话
		return $tmpInfo; // 返回数据
	}
	public function index(){
		$post=IS_POST && !empty($_POST);
		$get=IS_GET && !empty($_GET);
		if($post or $get){
			$param=I('param.','','trim');
			$url1="https://www.kuaidi100.com/query?id=1&valicode=&temp=0.6389174095820636";
			$text=$param['mail_no'];
			// $text='401097868964';
			$res=$this->ems_auto($text);
			$data=json_decode($res,true);
			// dump($data);
			// die;
			$data_auto=$data['auto'][0];
			$type=$data_auto['comCode'];
			$postid=$data['num'];
			$postid=$text;
			$url1.="&type=".$type;
			$url1.="&postid=".$postid;
			$url2="http://www.kuaidi100.com/";
			$tmpInfo=$this->curl_web($url1,$url2);
			// dump($tmpInfo);
			// die;
			$list=json_decode($tmpInfo,true);
			$this->assign('list',$list);
			$this->assign('kd',$data['auto'][0]['comCode']);
			$this->display();
			
		}else{
			$this->assign('list',1);
			$this->display();
		}
	}
	public function ems_auto($text){
			$url1="https://www.kuaidi100.com/autonumber/autoComNum?text=";
			// $text='401097868964';
			$url1.=$text;
			$url2="http://www.kuaidi100.com/";
			// dump($url);
			// die;
			$tmpInfo=$this->curl_web($url1,$url2);
			return $tmpInfo;
	}
    // public function index(){
		// $post=IS_POST && !empty($_POST);
		// $get=IS_GET && !empty($_GET);
		// if($post or $get){
			// $m=M('EmsLog', C('DB_PREFIX_T'), C('db_config'));
			// $po=M('Pushorder', C('DB_PREFIX_T'), C('db_config'));
			// $param=I('param.','','trim');
			// $mail_no=$param['mail_no'];   
			// $map['logistics_no'] = $mail_no;//面单号
			// $info_p=$po->where($map)->find();
			// if(empty($info_p)){
				// $this->error('没有此运单号');
			// }else{
				// $list_m=$m->where(array('order_id'=>$info_p['id']))->select();
				// $this->assign('list',$list_m);
				// $this->display();
			// }
		// }else{
			// $this->assign('list',1);
			// $this->display();
		// }
    // }
	public function ems_list(){
		$m=M('EmsCompany',C('DB_PREFIX_T'),C('db_config'));
		$param=I('param.','','trim');
		$name=$param['name'];
		if(!empty($name)){
			$map['name'] = array('like','%'.$name.'%');
        }
		$map['status']=1;
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
		
		$count=$m->where($map)->count();
		$ems_list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		
		$this->assign('ems_list',$ems_list);
		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }
	//添加
	public function add_ems(){
		if(IS_POST){
			$m=M('EmsCompany', C('DB_PREFIX_T'), C('db_config'));
			$param=I('param.','','trim');
			$param['add_time']=time();
			$param['status']=1;
			$res=$m->add($param);
			if($res){
				$return['message']='添加成功';
				$return['status']=true;
				$this->ajaxReturn($return);
			}else{
				$return['message']='添加失败';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
		}else{
			$this->display();
		}
	}
	//编辑
	public function edit_ems(){
		$m=M('EmsCompany', C('DB_PREFIX_T'), C('db_config'));
		$id=trim(I('id'));
		if(IS_POST){
			$param=I('param.','','trim');
			unset($param['id']);
			$param['update_time']=time();
			$res=$m->where(array('id'=>$id))->save($param);
			if($res!==false){
				$return['message']='编辑成功';
				$return['status']=true;
				$this->ajaxReturn($return);
			}else{
				$return['message']='编辑失败';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
		}else{
			$detail=$m->where(array('id'=>$id))->find();
			$this->assign('detail',$detail);
			$this->display();
		}
	}
	//删除
	function del_ems(){
		$id=trim(I('id'));
		$m=M('EmsCompany', C('DB_PREFIX_T'), C('db_config'));
		$map['id']=$id;
		$status=$m->where($map)->delete();
		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	
	public function emsfee_list(){
		$m=M('EmsFee',C('DB_PREFIX_T'),C('db_config'));
		$param=I('param.','','trim');
		$area=$param['area'];
		$code=$param['code'];
		if(!empty($area)){
			$map['area'] = array('like','%'.$area.'%');
        }
		if(!empty($code)){
			$map['company_code'] = $code;
        }
		// dump($param);
		$map['status']=1;
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
		
		$count=$m->where($map)->count();
		$ems_list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		// echo $m->getlastsql();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		
		$this->assign('ems_list',$ems_list);
		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }
	//添加
	public function add_emsfee(){
		if(IS_POST){
			$m=M('EmsFee', C('DB_PREFIX_T'), C('db_config'));
			$param=I('param.','','trim');
			
			$param['company_code']=I('code');
			$param['add_time']=time();
			$param['status']=1;
			// dump($param);
			$info=$m->where(array('area'=>$param['area']))->find();
			if(empty($param['first_weight'])){
				$return['message']='首重不能为空';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			if(empty($param['first_price'])){
				$return['message']='首重单位价格不能为空';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			if(empty($param['second_weight'])){
				$return['message']='续重不能为空';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			if(empty($param['second_price'])){
				$return['message']='续重单位价格不能为空';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			if($info){
				$return['message']='该目的地已经存在';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			$res=$m->add($param);
			// echo $m->getlastsql();
			if($res){
				$return['message']='添加成功';
				$return['status']=true;
				$this->ajaxReturn($return);
			}else{
				$return['message']='添加失败';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
		}else{
			$province_list=province_list();
			$this->assign('province_list',$province_list);
			$this->display();
		}
	}
	//邮费
	public function ems_fee(){
		$m=M('EmsFee', C('DB_PREFIX_T'), C('db_config'));
		$param=I('param.','','trim');
		$weight=$param['weight'];
		$area=$param['area'];
		$map['company_code']=$param['company_code'];
		$map['area']=array('like',"%$area%");
		$m_info=$m->where($map)->find();
		if(empty($m_info)){
			$return['message']='未设置快递费用模板，请先设置';
			$return['status']=0;
			$this->ajaxReturn($return);
		}else{
			$de_weight=$m_info['first_weight']-$weight;
			//首重
			if($de_weight>=0){
				$return['total'] = $m_info['first_weight']*$m_info['first_price'];//首重价格（起步价）
				$return['status']=1;
				$this->ajaxReturn($return);
			}else{
				$remain_weight=$weight-$m_info['first_weight'];//减去首重的剩余重量
				$second_total_price=(floor($remain_weight/$m_info['second_weight'])+1)*$m_info['second_weight']*$m_info['second_price'];//续重价格
				$return['total'] = $m_info['first_weight']*$m_info['first_price']+$second_total_price;
				$return['status']=1;
				$this->ajaxReturn($return);
			}
		}
	}
	//编辑
	public function edit_emsfee(){
		$m=M('EmsFee', C('DB_PREFIX_T'), C('db_config'));
		$id=trim(I('id'));
		if(IS_POST){
			$param=I('param.','','trim');
			unset($param['id']);
			$param['update_time']=time();
			$res=$m->where(array('id'=>$id))->save($param);
			if($res!==false){
				$return['message']='编辑成功';
				$return['status']=true;
				$this->ajaxReturn($return);
			}else{
				$return['message']='编辑失败';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
		}else{
			$province_list=province_list();
			$this->assign('province_list',$province_list);
			$detail=$m->where(array('id'=>$id))->find();
			$this->assign('detail',$detail);
			$this->display();
		}
	}
	//删除
	function del_emsfee(){
		$id=trim(I('id'));
		$m=M('EmsFee', C('DB_PREFIX_T'), C('db_config'));
		$map['id']=$id;
		$status=$m->where($map)->delete();
		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
}